package com.df.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.lang.reflect.Field;
import java.util.*;

public class MyQueryWrapper4Like<T> extends QueryWrapper<T> {


    public MyQueryWrapper4Like() {
        super();
    }

    public MyQueryWrapper4Like(T entity) {
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            // 设置可访问，允许访问私有字段
            field.setAccessible(true);
            try {
                Object value = field.get(entity);
                if (value instanceof String) {
                    if(value.equals("")) {
                        field.set(entity, null);
                    }else if(CommonUtils.varIsNotBlank(value)){
                        String columnName = field.getName(); // 实际应用中应该通过某种方式获取真实的列名
                        this.like(columnName, value);
                    }
                }
            } catch (Exception ignored) {
            }
        }

        super.setEntity(entity);
        super.initNeed();
    }

    /**
     * 实体类中的String类字段模糊查询
     * @param entity 实体类查询线索
     * @return 查询条件 MyQueryWrapper4Like
     */
    public MyQueryWrapper4Like<T>stringLike(T entity) {
        MyQueryWrapper4Like<T> wrapper=new MyQueryWrapper4Like<>();
        Set<Field> fields = new HashSet<>();
        Class<?> object = entity.getClass();
        while (object != null) {
            fields.addAll(Arrays.asList(object.getDeclaredFields()));
            object = object.getSuperclass();
        }
        for (Field field : fields) {
            // 设置可访问，允许访问私有字段
            field.setAccessible(true);
            try {
                String columnName = field.getName(); // 实际应用中应该通过某种方式获取真实的列名

                System.out.println(field.getName());
                System.out.println(field.get(entity));
                Object value = field.get(entity);
                if (value instanceof String) {
                    if(value.equals("")) {
                        field.set(entity, null);
                    }else if(CommonUtils.varIsNotBlank(value)){
                        wrapper.like(columnName, value);
                    }
                } else {
                    wrapper.eq(columnName, value);
                }
            } catch (Exception ignored) {
            }
        }
        return wrapper;
    }

    @Override
    public MyQueryWrapper4Like<T> eq(String column, Object val) {
        super.eq(CommonUtils.varIsNotBlank(val), column, val);
        return this;
    }

    @Override
    public MyQueryWrapper4Like<T> like(String column, Object val) {
        super.like(CommonUtils.varIsNotBlank(val), column, val);
        return this;
    }

    @Override
    public MyQueryWrapper4Like<T> between(String column, Object val1, Object val2) {
        super.between(CommonUtils.varIsNotBlank(val1, val2), column, val1, val2);
        return this;
    }

    @Override
    public MyQueryWrapper4Like<T> ge(String column, Object val) {
        super.ge(CommonUtils.varIsNotBlank(val), column, val);
        return this;
    }

    @Override
    public MyQueryWrapper4Like<T> le(String column, Object val) {
        super.le(CommonUtils.varIsNotBlank(val), column, val);
        return this;
    }

    @Override
    public MyQueryWrapper4Like<T> orderByDesc(String column) {
        super.orderByDesc(column);
        return this;
    }

    @Override
    public MyQueryWrapper4Like<T> orderByAsc(String column) {
        super.orderByAsc(column);
        return this;
    }

}